package com.example.security.config;




import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2  //开启swagger2
public class SwaggerConfig {

    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2);

    }

    //配置Swagger的Docket的bean实例
    @Bean
    public Docket docket(Environment environment){
        //设置要显示的swagger环境
        Profiles profiles= Profiles.of("dev","test","pro");
        //获取项目的环境
        //通过environment.acceptsProfiles判断是否处在自己设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);
        System.out.println(flag);

        return
                new Docket(DocumentationType.SWAGGER_2)
                        //本来是默认的，我们可以自己改，自己配置info
                .apiInfo(apiInfo())
                .groupName("yangss")
                //enable是否启动swagger，默认开启true,如果为false就不能再游览器中访问
                //.enable(flag)
                .select()
                //RequestHandlerSelectors，配置要扫描借口的方式
                         //basePackage()指定要扫描的包
                        //any()扫描全部
                        //none()都不扫描
                        //withClassAnnotation：扫描类上的注解，参数是一个注解的反射对象
                            // 只扫描有这个注解的
                           //withClassAnnotation(RestController.class)

                        //withMethodAnnotation：扫描方法上的注解
                          // 例如withMethodAnnotation(GetMapping.class)
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                //paths:过滤什么路劲
                 //.paths(PathSelectors.ant("/example/**"))
                 .build();

    }



    //配置swagger信息apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("yangss", "https://baidu.com", "yangshuisong@qq.com");
        return new ApiInfo(
                "api日记",
                "作者真帅",
                "v1.0",
                "https://baidu.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}
